package com.demo.framework.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 
* @ClassName: StringUtil 
* @Description: TODO(字符串工具类) 
* @author Administrator 
* @date 2013-3-30 下午10:16:48 
* @version 1.0
 */
public class StringUtil {
    
	/**
	* @Title: isEmpty 
	* @Description: TODO(判断字符串是否为空) 
	* @param @param str
	* @param @return    设定文件 
	* @return boolean    返回类型  为空时 返回为 true
	* @throws
	 */
	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	 /**
     * 
    * @Title: is_str_blank 
    * @Description: TODO(判断字符串非空) 
    * @param @param str
    * @param @return
    * @return boolean  如果为空 或 null  返回true 
    * @throws
     */
	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0)
			return true;
		for (int i = 0; i < strLen; i++)
			if (!Character.isWhitespace(str.charAt(i)))
				return false;

		return true;
	}

	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}
  
	/**
	* @Title: encode 
	* @Description: TODO(对字符进行转码) 
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
    public static String encode(String s) {
        if (s == null) {
            return "";
        }
        try {
            return URLEncoder.encode(s, "UTF-8").replace("+", "%20")
                    .replace("*", "%2A").replace("%7E", "~")
                    .replace("#", "%23");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

   /**
   * @Title: decode 
   * @Description: TODO(对URL decoder 进行反编码) 
   * @param @return    设定文件 
   * @return String    返回类型 
   * @throws
    */
    public static String decode(String s) {
        if (s == null) {
            return "";
        }
        try {
            return URLDecoder.decode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    
    /**
    * @Title: isEmail 
    * @Description: TODO(判断字符串 是否为正确的 Email 格式) 
    * @param @return    设定文件 
    * @return boolean    返回类型 
    * @throws
     */
	public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }
    /**
     * 检测字符串中是否包含汉字
     * @param sequence
     * @return
     */
    public static boolean checkChinese(String sequence) {
        final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]";
        boolean result = false;
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(sequence);
        result = matcher.find();
        return result;
    }
    
    /**
     * 检测字符串中只能包含：中文、数字、下划线(_)、横线(-)
     * @param sequence
     * @return
     */
      public static boolean checkNickname(String sequence) {
            final String format = "[^\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w-_]";
            Pattern pattern = Pattern.compile(format);
            Matcher matcher = pattern.matcher(sequence);
            return !matcher.find();
        } 
}
